package com.jie.demo1.controller.uploadController;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jie.demo1.Sservice.FileInformationTableService;
import com.jie.demo1.Sservice.FileTypeTableService;
import com.jie.demo1.Sservice.UploadService;
import com.jie.demo1.Sservice.UserService;
import com.jie.demo1.Sservice.impl.UserServiceImpl;
import com.jie.demo1.common.ulity.JWTUtil;
import com.jie.demo1.pojo.FileInformationTable;
import com.jie.demo1.pojo.FileTypeTable;
import com.jie.demo1.pojo.Result;
import io.jsonwebtoken.Claims;
import io.minio.errors.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@CrossOrigin(origins = "http://localhost:5173/")  //开启跨域请求
@RestController
@RequestMapping("/upload")
//关于用户上传文件接口
public class userUploadController {
    @Autowired
    private FileTypeTableService fileTypeTableService;

    @Autowired
    private FileInformationTableService fileInformationTableService ;

    @Autowired
    private UploadService uploadService;
    //使用minio上传用户头像
    //一般情况下，controller层不会抛出异常，因为一般需要在controller中处理，但是这里使用全局异常处理，所以需要抛出异常由全局异常处理来处理
    @PostMapping("avatar")
    public Result uploadUserHead(@RequestBody MultipartFile multipartFile) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //将文件存放入minio中并返回minio中文件的url
       String image_url = uploadService.uploadImage(multipartFile);
        return Result.ok(image_url);
    }

    //前端会上传一个文件，文件描述信息，请求头中携带token，通过解析token获得到用户信息，然后将文件信息和用户信息存入数据库中
    @PostMapping("/addFileTableInformation")
    public Result addFileTableInformation(@RequestPart(value = "file1") MultipartFile multipartFile,
                                          @RequestHeader("Authorization") String authorization) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        System.out.println("------------------开始--------------------------");
        System.out.println(multipartFile);
        Long size = multipartFile.getSize();//获取文件大小
        String originalFilename = multipartFile.getOriginalFilename();  //获取文件名
        String contentType = multipartFile.getContentType();//获取文件类型,是在文件头中获取的MIME类型
        //根据文件类型获取文件类型id
        System.out.println("------------------根据文件类型获取文件类型id--------------------------");
        FileTypeTable fileTypeTable = fileTypeTableService.getFileTypeTableById(contentType);
        Integer typeId = fileTypeTable.getTypeId();
        //将文件存放入minio中并返回minio中文件的url
        System.out.println("------------------将文件存放入minio中并返回minio中文件的url--------------------------");
        String image_url = uploadService.uploadImage(multipartFile);
        //获取token中携带的用户信息
        System.out.println("------------------获取token中携带的用户信息--------------------------");
        String token = authorization.substring(7); //获取token
        System.out.println("token:"+token);

        //解析token获取用户信息
        Claims claims = JWTUtil.checkToken(token);
        String upload_user = claims.get("username", String.class);

        //将文件信息和用户信息存入数据库中
        FileInformationTable NEW_fileInformationTable = new FileInformationTable();
        NEW_fileInformationTable.setUploadUser(upload_user);
        NEW_fileInformationTable.setStorageUrl(image_url);
        NEW_fileInformationTable.setFileName(originalFilename);
        NEW_fileInformationTable.setTypeId(typeId);
        NEW_fileInformationTable.setFileSize(size);
        fileInformationTableService.save(NEW_fileInformationTable);
        return Result.ok(NEW_fileInformationTable);
    }
}
